/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is RhinoDB, released August 08, 2008.
 * 
 * Contributor(s): Alexandru Chiculita.
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 * 
 * ***** END LICENSE BLOCK ***** */


package org.achicu.db;

import java.util.HashMap;

import org.achicu.db.rhino.RhinoStorableObject;
import org.achicu.db.values.StorableInteger;
import org.achicu.db.values.StorableNumber;
import org.achicu.db.values.StorableObject;
import org.achicu.db.values.StorableString;
import org.achicu.db.values.StorableValue;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;

public class StorableValueFactory {

	protected HashMap storableTypes = new HashMap();  
	
	private static StorableValueFactory instance;
	
	public static StorableValueFactory getInstance(){
		if(instance==null){
			instance = new StorableValueFactory();
		}
		return instance;
	}
	
	protected StorableValueFactory(){
		storableTypes.put(new Integer(StorableObject.TYPE), StorableObject.class);
		storableTypes.put(new Integer(StorableString.TYPE), StorableString.class);
		storableTypes.put(new Integer(StorableInteger.TYPE), StorableInteger.class);
		storableTypes.put(new Integer(StorableNumber.TYPE), StorableNumber.class);
		storableTypes.put(new Integer(StorableCollection.TYPE), StorableCollection.class);
		
	}
	
	public StorableValue getStorableValueByType(int type) throws InstantiationException, IllegalAccessException{
		Class objClass = (Class)storableTypes.get(new Integer(type));
		
		if( objClass == null)
			return null;
		
		StorableValue result = (StorableValue)objClass.newInstance();
		
		return result;
	}
	
	public static StorableValue wrapValue(Object value){
		if(value instanceof Integer){
			return new StorableInteger(((Integer)value).intValue());
		}else if(value instanceof Double){
			return new StorableNumber(((Double)value).doubleValue());
		}else if(value instanceof String){
			return new StorableString((String)value);
		}else if(value instanceof NativeJavaObject){
			Object unwrappedValue = ((NativeJavaObject)value).unwrap();
			if(unwrappedValue instanceof StorableValue){
				return (StorableValue)unwrappedValue;
			}else if(unwrappedValue instanceof String){
				return new StorableString((String)unwrappedValue);
			}
		}else if(value instanceof RhinoStorableObject){
			return ((RhinoStorableObject)value).getStorableObject();
		}else if(value instanceof StorableValue){
			return (StorableValue)value;
		}
		return null;
	}

	public static Object unwrapValue(StorableValue value, Scriptable scope) {
		if(value instanceof StorableObject){
			return new RhinoStorableObject((StorableObject)value, scope);
		}else if(value instanceof StorableCollection){
			return Context.javaToJS(value, scope);
		}
		return value.unwrap();
	}
	
}
